package com.example.administrator.testservice;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

public class MusicService extends Service {

    private MediaPlayer player;
    private String mp3Path="";
    private boolean isPause=false;
    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player=new MediaPlayer();
        mp3Path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/mp3/";
        Log.i("path",mp3Path);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action=intent.getStringExtra("action");
        switch(action){
            case "play":
                try{
                play();
                }
                catch(Exception e){

            }
                break;
            case "pause":
                pause();
                break;
        }

       // stopSelf();
      //  return super.onStartCommand(intent, flags, startId);
        return  START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    private void play() throws Exception{
        if(!isPause) {
            player.reset();
            player.setDataSource(mp3Path + "choubaguai.mp3");
            player.prepare();
        }
        player.start();
    }
    private void pause(){
        player.pause();
        isPause=true;
    }
}
